import { ApiCall } from "tsrpc";
import { prop_base } from "../../../model/bag/prop_base";
import { player } from "../../../model/fight/body/player";
import { ReqUse, ResUse } from "../../../shared/master/bag/PtlUse";
import { template } from "../../../shared/master/MsgAction";

export default async function (call: ApiCall<ReqUse, ResUse>) {
    call.succ({})

    let user = call.req._player as player;
    let useNum = Math.ceil(call.req.use);
    if (useNum <= 0) {
        useNum = 1;
    }
    let useIndex = call.req.idx;
    let resArry: (prop_base | undefined)[] = user.getBagList();
    if (useIndex > 0) {
        let useItem = resArry[useIndex - 1];
        if (!useItem) {
            user.sendMsg(template.default, `使用物品失败,没有在背包找到ID为${useIndex}的物品`)
            call.succ({})
            return;
        }
        useItem.use(user, useNum);
    } else {
        let useItem = resArry.find((item) => { return item && item?.getStrName() == call.req.name });
        if(!useItem){
            user.sendMsg(template.default, `使用物品失败,没有在背包找到名称为${call.req.name}的物品`)
            return;
        }
        useItem.use(user, useNum);
    }

}